package com.attendance.www.hr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.AttendanceBean;
import com.attendance.www.hr.beans.AttendancePageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.IAttendanceReportService;
import com.attendance.www.hr.service.IAttendanceService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 考勤记录
 * 
 * @version 2017/9/25-2017/9/27
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/attendance")
public class AttendanceController {

	@Resource
	private IAttendanceService attendanceService;
	@Resource
	private IAttendanceReportService attendanceReportService;
	@Resource
	private IApprovalService approvalService;

	/**
	 * 获取个人（或所有人）全部考勤接口
	 * 
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceList(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		Integer pageNo = attendanceBean.getPageNo();
		Integer pageCount = attendanceBean.getPageCount();
		if (pageNo == null || pageCount == null || attendanceBean.getAttendanceDateStart() == null
				|| attendanceBean.getAttendanceDateEnd() == null) {
			throw new ResultFaultException("0_00001");
		}
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		attendanceBean.setPrivilegeGroupId(userEmployee.getPrivilegeGroupId());
		AttendancePageBean attendancePageBean = new AttendancePageBean();
		attendancePageBean = attendanceService.getAttendanceList(attendanceBean, startNum, pageCount,
				userEmployee.getEmployeeId(), true);
		return ResultMap.success(attendancePageBean);
	}
	/**
	 * 获取个人考勤信息接口
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceListById.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceListById(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		Integer pageNo = attendanceBean.getPageNo();
		Integer pageCount = attendanceBean.getPageCount();
		if (pageNo == null || pageCount == null || attendanceBean.getAttendanceDateStart() == null
				|| attendanceBean.getAttendanceDateEnd() == null) {
			throw new ResultFaultException("0_00001");
		}
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		attendanceBean.setPrivilegeGroupId(2);
		AttendancePageBean attendancePageBean = new AttendancePageBean();
		attendancePageBean = attendanceService.getAttendanceList(attendanceBean, startNum, pageCount,
				userEmployee.getEmployeeId(), true);
		return ResultMap.success(attendancePageBean);
	}

	/**
	 * 获取个人（或所有人）考勤详细信息接口
	 * 
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceData.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceData(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		attendanceBean.setPrivilegeGroupId(userEmployee.getPrivilegeGroupId());
		if (attendanceBean.getAttendanceDateStart() == null || attendanceBean.getAttendanceDateEnd() == null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(attendanceService.getAttendanceData(attendanceBean, userEmployee.getEmployeeId()));
	}

	/**
	 * 导出数据
	 * 
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceURL(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		attendanceBean.setPrivilegeGroupId(userEmployee.getPrivilegeGroupId());
		return ResultMap.success(attendanceReportService.exportAttdanceReport(attendanceBean,userEmployee.getEmployeeId()));
	}

	/**
	 * 模板下载
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/downloadAttendanceURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object downloadAttendanceURL(HttpServletRequest request) {
		return AppConstant.TEMPLATE_RESOURCE + "/考勤导入模板.xlsx";
	}

	/**
	 * 考勤信息导入接口
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/importAttendance.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object importAttendance(HttpServletRequest request) throws Exception {
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		List<String> successList = attendanceService.importAttendance(Integer.parseInt(attachmentId));
		return ResultMap.success(successList);
	}

	/**
	 * 获取人员考勤数据次数
	 * 
	 * @param emploueeId
	 * @param date
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceCount.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceCount(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		int employeeId = userEmployee.getEmployeeId();
		if (attendanceBean.getAttendanceDate() == null) {
			throw new ResultFaultException("0_00001");

		}
		return ResultMap.success(attendanceService.getAttendanceCount(employeeId, attendanceBean.getAttendanceDate()));
	}

	/**
	 * 根据考勤Id来获取当前日期的所有申请
	 * 
	 * @param AttendanceId
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceRight.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceRight(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		return ResultMap.success(attendanceService.getAttendanceRight(attendanceBean));
	}

	/**
	 * 根据考勤Id和起始时间结束时间来重置考勤信息
	 */
	@RequestMapping(value = "/resetAttendance.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object resetAttendance(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		if (attendanceBean.getStartDate() == null || attendanceBean.getEndDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		approvalService.resetAttendance(attendanceBean.getStartDate(), attendanceBean.getEndDate(),
				attendanceBean.getEmployeeIdList());
		return ResultMap.success();
	}

	/**
	 * 根据考勤Id来撤回补签信息
	 * 
	 * @param attendanceId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/retractAttednance.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object retractAttednance(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		if (attendanceBean.getAttendanceList() == null) {
			throw new ResultFaultException("0_00001");
		}
		for(int i=0;i<attendanceBean.getAttendanceList().size();i++) {
			AttendanceBean ab=attendanceBean.getAttendanceList().get(i);
			attendanceService.retractAttednance(ab.getAttendanceId(),ab.getType());
		}
		return ResultMap.success();
	}

	/**
	 * 获取原始打卡时by日期
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceTimeByDate.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceTimeByDate(HttpServletRequest request, @RequestBody AttendanceBean attendanceBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (attendanceBean.getAttendanceDate() == null) {
			throw new ResultFaultException("0_00001");
		}
		attendanceBean.setEmployeeId(userEmployee.getEmployeeId());
		return ResultMap.success(attendanceService.getAttendanceTimeByDate(attendanceBean));
	}
}
